function out = f_ztest(p1,p2,N1,N2)
% p0 = (x1+x2)./(N1+N2); % pooled estimate of proportion
% n10 = N1*p0; % null hypothesis
% n20 = N2*p0; 
% observed = [x1 N1-x1 x2 N2-x2];
% expected = [n10 N1-n10 n20 N2-n20];
% out.chi2stat = sum((observed-expected).^2./expected);
% out.p = 1-chi2cdf(out.chi2stat,1);

if p1<p2
    out.z=(p2-p1)/sqrt( p2*(1-p2)/N2+p1*(1-p1)/N1);
else
    out.z=(p1-p2)/sqrt( p2*(1-p2)/N2+p1*(1-p1)/N1);
end

out.p=2*(1-normcdf(out.z,0,1));